package com.lipaluma.field.validator.impl.generic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import com.lipaluma.field.validator.ValidationError;
import com.lipaluma.field.validator.Validator;

/**
 * Validator ckeck the entry value is included in values defined by user.<br/>
 * <p>
 * To initialize isIncludedIn Validator : 
 * <ul>
 * <li>With annotation : add {@link IsIncludedIn} annotation to the field</li>
 * <li>on xml file : add element <br/><br/>
 * <code>&lt;isIncludedIn pattern="pattern"><br/>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;value>value 1&lt;/value><br/>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;value>value 2&lt;/value><br/>
 * &nbsp;&nbsp;&nbsp;&nbsp;...<br/>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;value>value n&lt;/value><br/>
 * &lt;/isIncludedIn></code><br/><br/>
 * in <code>&lt;column></code> element.<br/>
 * The attribute pattern is optional except in case values are date.
 * </li>
 * </ul>
 * 
 * @author Mario<br/>
 * <br/>
 * <b>The code in inspired by : </b>
 * @author Dominique De Vito from super-csv
 * @author James Bassett from super-csv
 */

public abstract class AbstractIsExcludedOfValidator<T> implements Validator {

	private Collection<T> possibleValues= new ArrayList<T>();
	
	public AbstractIsExcludedOfValidator() {}
	public AbstractIsExcludedOfValidator(Collection<T> values) {
		init(values);
	}
	public AbstractIsExcludedOfValidator(T... values) {
		init(Arrays.asList(values));
	}

	protected void init(Collection<T> values) {
		possibleValues.addAll(values);		
	}
	
	public boolean validate(Object value, List<ValidationError> errors) {
		if(possibleValues.contains(value)) {
			errors.add(new ValidationError("The value "+value+" must not be included in "+possibleValues));
			return false;
		}
		return true;
	}

}
